<template>
  <div>
    <!-- 面包屑   -->
    <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">系统管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--   用户列表 -->
    <el-card class="box-card">
      <el-form :inline="true" :model="form">
        <el-form-item label="昵称" label-width="70px">
          <el-input clearable v-model="form.userName"></el-input>
        </el-form-item>
        <el-form-item label="性别" label-width="70px">
          <el-radio v-model="form.userAge" label="0">男</el-radio>
          <el-radio v-model="form.userAge" label="1">女</el-radio>
        </el-form-item>
        <el-form-item style="margin-left:50px;">
          <el-button @click="" icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" @click="" icon="el-icon-search">查询</el-button>
          <el-button type="success" icon="el-icon-plus" @click="addvisible = true">添加
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 添加的对话框 -->
      <el-dialog :visible.sync="addvisible" title="添加数据">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="form.userName"></el-input>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.userAge"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-input v-model="form.userSex"></el-input>
          </el-form-item>
          <el-form-item label="锁定">
            <el-input v-model="form.userLock"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit(form)">立即创建</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!--  穿梭框-->
      <el-dialog :visible.sync="Transfervisible" title="设置角色">
        <template>
          <el-transfer v-model="yesData" :data="noData" :titles="['所有角色', '拥有角色']"
                       :button-texts="['移除角色', '添加角色']"
                       :props="{key: 'id',label: 'roleName'}"
                       @change="handleChange"
          ></el-transfer>
        </template>
      </el-dialog>
      <!-- 修改对话框 -->
      <el-dialog :visible.sync="visible" title="修改数据">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="id">
            <el-input v-model="form.id"></el-input>
          </el-form-item>
          <el-form-item label="用户名">
            <el-input v-model="form.userName"></el-input>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.userAge"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-input v-model="form.userSex"></el-input>
          </el-form-item>
          <el-form-item label="锁定">
            <el-input v-model="form.userLock"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updateSubmit(form)">立即创建</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!--表格-->
      <el-table :data="userList" border style="width: 100%;">
        <el-table-column prop="id" label="ID" width="50">
        </el-table-column>
        <el-table-column prop="userName" label="名字" width="110">
        </el-table-column>
        <el-table-column prop="userAge" label="年龄" width="150">
        </el-table-column>
        <el-table-column prop="userQiandao" label="是否签到" width="150">
          <template slot-scope="scope">
            {{ scope.row.userQiandao == 0 ? '未签到' : '已签到' }}
          </template>
        </el-table-column>
        <el-table-column prop="userSex" label="性别" width="150">
          <template slot-scope="scope">
            {{ scope.row.userSex == 0 ? '男' : '女' }}
          </template>
        </el-table-column>
        <el-table-column prop="userTel" label="账号" width="150">
        </el-table-column>
        <el-table-column prop="userLock" label="是否锁定" width="150">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.userLock==1"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="updateSwitch(scope.$index, scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope)" icon="el-icon-edit"></el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.$index, scope.row)"
                       icon="el-icon-delete"></el-button>
            <el-button type="success" size="mini" @click="Transfervisible = true" icon="el-icon-s-tools"></el-button>
            <el-button type="warning" size="mini" icon="el-icon-refresh-left"
                       @click="handleUpdate(scope.$index, scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination style=" margin-top:15px;
            " @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="current" :page-sizes="[5, 10]" :page-size="pageSize"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { findUsers } from '../../api/users.js'
import axios from 'axios'
import qs from 'qs'
import swal from 'sweetalert'

export default {
  name: 'Users',
  data () {
    return {
      //左边数据
      noData: [],
      //右边数据
      yesData: [],
      // 注意:key 的字符类型要一致!!!
      addvisible: false,
      visible: false,
      dialogVisible: false,
      Transfervisible: false,

      form: {
        id: '',
        userName: '',
        userAge: '',
        userQiandao: '',
        userSex: '',
        userLock: '',
      },
      //用户集合
      userList: [],
      //当前页码
      current: 1,
      //每条页数
      pageSize: 5,
      //总条数
      total: 100,
    }
  },
  created () {
    //创建组件的时候调用fangfa
    this.getFindUsers(),
      axios.get('http://localhost:8081/roles/getAllList',   // url 以自己的地址为准
      ).then(res => {
        let arr = []
        for (let i in res.data) {
          arr.push(res.data[i])
        }
        console.log(arr)
        this.noData = arr

      })
  },
  methods: {
    handleChange (value, direction, movedKeys) {
      console.log(value, direction, movedKeys)
      //可以通过direction回调right/left 来进行操作，right：把数字移到右边，left把数据移到左边
      if (direction === 'right') {

      }
      if (direction === 'left') {

      }

    },
    handleEdit (o) { //编辑
      console.log(o.row)
      console.log(o.row.id)
      this.visible = true
      this.form = o.row
    },
    updateSwitch (index, row) {
      console.log('测试')
      if (row.userLock == 1) {
        axios.post('http://localhost:8081/uesr/setUnLock',   // url 以自己的地址为准
          qs.stringify({
            id: row.id,
          })
        ).then(res => {

          location.reload()

        })
      } else if (row.userLock == 0) {
        axios.post('http://localhost:8081/uesr/setLock',   // url 以自己的地址为准
          qs.stringify({
            id: row.id,
          })
        ).then(res => {

          location.reload()

        })
      }
    },
    handleUpdate (index, row) { //删除
      console.log(index, row)
      this.$confirm('确定重置该用户密码？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.post('http://localhost:8081/uesr/updatePwas',   // url 以自己的地址为准
          qs.stringify({
            id: row.id,
          })
        ).then(function (ret) {
          console.log(row.id)
          console.log(ret.data)
        })
        this.$message({
          type: 'success',
          message: '重置成功!'

        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消重置'
        })
      })
    },
    handleDelete (index, row) { //删除
      console.log(index, row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.post('http://localhost:8081/uesr/delete',   // url 以自己的地址为准
          qs.stringify({
            id: row.id,
          })
        ).then(function (ret) {
          console.log(row.id)
          console.log(ret.data)
        })
        this.$message({
          type: 'success',
          message: '删除成功!'

        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
    ,
    onSubmit (form) {
      axios.post('http://localhost:8081/uesr/insert',   // url 以自己的地址为准
        qs.stringify({
          userName: form.userName,
          userAge: form.userAge,
          userQiandao: form.userQiandao,
          userSex: form.userSex,
          userLock: form.userLock,
        })
      ).then(res => {
        swal({
          title: '成功',
          icon: 'success',
          buttons: false,
          timer: 2000,
        })
        setTimeout(function () {
          location.reload()
        }, 2000)
      }).catch(error => {
        swal({
          title: '失败',
          icon: 'error',
          buttons: false,
          timer: 2000,
        })
        setTimeout(function () {
          location.reload()
        }, 2000)
      })
    },
    updateSubmit (form) {
      axios.post('http://localhost:8081/uesr/update',   // url 以自己的地址为准
        qs.stringify({
          id: form.id,
          userName: form.userName,
          userAge: form.userAge,
          userQiandao: form.userQiandao,
          userSex: form.userSex,
          userLock: form.userLock,
        })
      ).then(res => {
        swal({
          title: '成功',
          icon: 'success',
          buttons: false,
          timer: 2000,
        })
        setTimeout(function () {
          location.reload()
        }, 2000)
      }).catch(error => {
        swal({
          title: '失败',
          icon: 'error',
          buttons: false,
          timer: 2000,
        })
        setTimeout(function () {
          location.reload()
        }, 2000)
      })
    },
    //当每页条数改变的时候
    handleSizeChange (val) {
      //将val赋值给size
      this.size = val
      //重新去后台查询数据
      this.getFindUsers()

    },
    //当前页码改变的时候
    handleCurrentChange (val) {
      this.current = val
      //重新去后台查询数据
      this.getFindUsers()
    },
    async getFindUsers () {
      const { data } = await findUsers(this.current, this.size)
      this.userList = data.data.records
      this.total = data.data.total
    }
    // async getList () {
    //   const { data } = await getAllList(
    //   )
    //   this.noData = data.data.data
    //
    // }
  }
}
</script>
<style scoped>
</style>
